package com.snail.aspect;

import com.snail.exception.SellException;
import com.snail.exception.SellerAuthorizeException;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author 【swg】.
 * @Date 2017/12/11 21:27
 * @DESC
 * @CONTACT 317758022@qq.com
 */
@Aspect
@Component
@Slf4j
public class SellerAuthorizeAspect {

    @Pointcut("execution(public * com.snail.controller.Seller*.*(..))" +
            "&& !execution(public * com.snail.controller.SellerInfoController.*(..))")
    public void verify(){}

    @Before("verify()")
    public void doVerify(){
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

        if(request.getSession().getAttribute("loginUser") == null){
            log.warn("【登陆校验】,用户未登陆");
            throw new SellerAuthorizeException();
        }
    }
}
